import { /* inject, */ BindingScope, inject, injectable} from '@loopback/core';
import {repository} from '@loopback/repository';
import {TrainingDataSource} from '../datasources';
import {Employ} from '../models';
import {EmployRepository} from '../repositories';

@injectable({scope: BindingScope.TRANSIENT})
export class EmployService {
  constructor(
    @repository(EmployRepository)
    public employRepository: EmployRepository,

    @inject('datasources.training') private dataSource: TrainingDataSource
  ) { }

  /*
   * Add service methods here
   */

  // 创建测试
  createEmploy(employ: Employ) {
    return this.employRepository.create(employ);
  }

  // 查询测试
  async findEmploy(employ: Employ) {
    return this.employRepository.find({
      where: {
        employeeNo: employ.employeeNo,
        name: employ.name,
      },
    });
    // TODO: 参考代码
    // const data = await this.employRepository.execute(`SELECT employee_no FROM Employ WHERE employee_no='${employ.employee_no}' and name=${employ.name}`) as Employ[]  // 可以使用
    // const data = this.dataSource.execute(`select employee_no from Employ where employee_no='${employ.employee_no}' and name='${employ.name}'`);   // 可以使用
    // console.log(data);
    // return data;
  }


  // get查询测试 写法
  async findEmployByGet(employeeNo: string) {
    return this.employRepository.find({
      where: {employeeNo}
    });
  }

  // 更新数据
  updateEmploy(employ: Employ, where: {employeeNo: string}) {
    return this.employRepository.updateAll(employ, where);
  }

  // 删除数据
  delectEmploy(where: {employeeNo: string}) {
    return this.employRepository.deleteAll(where);
  }
}
